﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace binary
{
    public partial class CopyFiles : Form
    {
        public ConfigList list;
        public Thread copyThread;
        public int copied = 0;
        public delegate void delProgress();
        public delegate void delClose();
        public SingleTable activeTable = new SingleTable();

        public CopyFiles()
        {
            InitializeComponent();
        }

        private void CopyFiles_Load(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "Wskaż plik listy";
            open.Filter = "Plik XML|*.xml";
            DialogResult result = open.ShowDialog();
            if(result == DialogResult.OK)
            {
                this.list = XMLReader.getImportList(open.FileName);
                lblSource.Text = String.Format("Folder źródłowy: {0}", list.dbfSource);
                lblDestination.Text = String.Format("Folder docelowy: {0}", list.dbfDestination);
                if (list.tableList.Count > 0)
                {
                    copyBar.Maximum = this.list.tableList.Count;
                    this.copyThread = new Thread(new ThreadStart(doCopyFiles));
                    copyThread.Start();
                }
                else
                    this.Close();
            }
            else
                this.Close();

        }

        /// <summary>
        /// Watek kopiujacy pliki
        /// </summary>
        private void doCopyFiles()
        {
            string fileSource = "";
            string fileDestination = "";
            foreach (SingleTable file in this.list.tableList)
            {
                activeTable = file;
                fileSource = String.Format("{0}{1}.dbf", this.list.dbfSource, file);
                fileDestination = String.Format("{0}{1}.dbf", this.list.dbfDestination, file);
                if (File.Exists(fileSource))
                {
                    if (File.Exists(fileDestination))
                        File.Delete(fileDestination);
                    File.Copy(fileSource, fileDestination);   
                }
                if (File.Exists(fileSource.Replace(".dbf", ".fpt")))
                {
                    if (File.Exists(fileDestination.Replace(".dbf", ".fpt")))
                        File.Delete(fileDestination.Replace(".dbf", ".fpt"));
                    File.Copy(fileSource.Replace(".dbf", ".fpt"), fileDestination.Replace(".dbf", ".fpt"));
                }
                Invoke(new delProgress(setProgress));
            }
            Invoke(new delClose(CloseForm));
        }

        /// <summary>
        /// Ustawienie progressu
        /// </summary>
        public void setProgress()
        {
            this.copied++;
            lblCopy.Text = String.Format("Trwa kopiowanie plików: {0} z {1} - {2}", copied, list.tableList.Count, activeTable.name);
            copyBar.Value++;
        }

        /// <summary>
        /// Zamkniecie okna
        /// </summary>
        public void CloseForm()
        {
            while (copyThread.ThreadState == ThreadState.Running)
            {
                Thread.Sleep(100);
            }
            this.DialogResult = DialogResult.OK;
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lblCancel.Visible = true;
            btCancel.Enabled = false;
            this.Refresh();
            this.copyThread.Abort();
            this.copyThread.Join();
            this.Close();
        }

    }
}
